/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marsaMaroc.controllers;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import marsaMaroc.dao.GraphiquesDAO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author Administrateur
 */
public class BarChart extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    GraphiquesDAO graphiquesDAO = new GraphiquesDAO();
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    int janvier = graphiquesDAO.selectReclamationBetween("2014-01-01", "2014-01-31");
    int fevrier = graphiquesDAO.selectReclamationBetween("2014-02-01", "2014-02-30");
    int mars = graphiquesDAO.selectReclamationBetween("2014-03-01", "2014-03-31");
    int avril = graphiquesDAO.selectReclamationBetween("2014-04-01", "2014-04-30");
    int mai = graphiquesDAO.selectReclamationBetween("2014-05-01", "2014-05-31");
    int juin = graphiquesDAO.selectReclamationBetween("2014-06-01", "2014-06-30");
    int juillet = graphiquesDAO.selectReclamationBetween("2014-07-01", "2014-07-31");
    int aout = graphiquesDAO.selectReclamationBetween("2014-08-01", "2014-08-31");
    int septembre = graphiquesDAO.selectReclamationBetween("2014-09-01", "2014-09-30");
    int octobre = graphiquesDAO.selectReclamationBetween("2014-10-01", "2014-10-31");
    int novembre = graphiquesDAO.selectReclamationBetween("2014-11-01", "2014-11-30");
    int decembre = graphiquesDAO.selectReclamationBetween("2014-12-01", "2014-12-31");
    
    dataset.addValue(janvier, "Réclamation", "Janvier"); 
    dataset.addValue(fevrier, "Réclamation", "Février"); 
    dataset.addValue(mars, "Réclamation", "Mars"); 
    dataset.addValue(avril, "Réclamation", "Avril"); 
    dataset.addValue(mai, "Réclamation", "Mai");
    dataset.addValue(juin, "Réclamation", "Juin"); 
    dataset.addValue(juillet, "Réclamation", "Juillet"); 
    dataset.addValue(aout, "Réclamation", "Aout"); 
    dataset.addValue(septembre, "Réclamation", "Septembre");
    dataset.addValue(octobre, "Réclamation", "Octobre"); 
    dataset.addValue(novembre, "Réclamation", "Novembre"); 
    dataset.addValue(decembre, "Réclamation", "Décembre"); 
     

    JFreeChart barChart = ChartFactory.createBarChart("Evolution des Réclamation par mois", "", 
    "Nombre des Réclamation", dataset, PlotOrientation.VERTICAL, true, true, false); 
    barChart.setBackgroundPaint(Color.white);
    
    OutputStream out = response.getOutputStream(); 
    response.setContentType("image/png"); 
    ChartUtilities.writeChartAsPNG(out, barChart, 750, 300);
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
